//
//  DDUnitTestDemoTests.m
//  DDUnitTestDemoTests
//
//  Created by jiangzhenfeng on 2018/2/1.
//  Copyright © 2018年 jiangzhenfeng. All rights reserved.
//

#import <XCTest/XCTest.h>

@interface DDUnitTestDemoTests : XCTestCase

@property (nonatomic, strong) UIButton * button;

@end

@implementation DDUnitTestDemoTests

- (void)setUp {
    [super setUp];
    // Put setup code here. This method is called before the invocation of each test method in the class.
    
    // 测试前的准备，在测试case执行开始前被调用  相当于 @Before
    NSLog(@"%s",__func__);
    
    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
    
    
}

- (void)tearDown {
    // Put teardown code here. This method is called after the invocation of each test method in the class.
    [super tearDown];
    // 测试执行完成后，在测试case执行结束后被调用  相当于 @After
    NSLog(@"%s",__func__);
}

- (void)testExample {
    // This is an example of a functional test case.
    // Use XCTAssert and related functions to verify your tests produce the correct results.
    NSLog(@"%s",__func__);
}

- (void)testPerformanceExample {
    // This is an example of a performance test case.
    
    //性能测试方法，通过测试block中方法执行的时间，比对设定的标准值和偏差觉得是否可以通过测试
    NSLog(@"%s",__func__);
    
    [self measureBlock:^{
        // Put the code you want to measure the time of here.
    }];
    
}

#pragma mark 各种常用Assert测试
- (void)testAssertExample
{
    // 通用 Assert
    XCTAssert(YES,@"这里与我们常用的Assert一样，NO 或者 0 就意味着测试不通过");
    // 常用 Assert
    XCTAssertTrue(1 + 1 > 1, @"这里期望结果为 true ,否则就是测试不通过");
    XCTAssertFalse(1 + 1 < 1, @"这里期望结果为 failed ,否则就是测试不通过");
    XCTAssertNil(nil, @"这里期望结果为 nil ,否则就是测试不通过");
    XCTAssertEqual(1, 1, @"这里期望 两个值或者对象相等 ,否则就是测试不通过");
    XCTAssertEqual(@"1", @"1", @"这里期望 两个值相等 ,否则就是测试不通过");
    XCTAssertEqualObjects(@"success", @"success", @"这里期望 两个对象相等 ,否则就是测试不通过");
    // 直接玩完
//    XCTFail(@"这里是无条件挂掉，一般是前面有一系列的条件，走到这一步意味着严重超出了你的预期");
    
}

#pragma mark 异步测试
- (void)testAsynchronousURLConnection {
    XCTestExpectation *expectation = [self expectationWithDescription:@"GET 方法访问达达官网"];
    
    NSURL *url = [NSURL URLWithString:@"https://www.imdada.cn/"];
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionDataTask *task = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        XCTAssertNotNil(data, @"返回数据不应该为 nil");
        XCTAssertNil(error, @"error 应该为 nil");
        if (nil != response) {
            NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
            XCTAssertEqual(httpResponse.statusCode, 200, @"HTTPResponse的状态码应该是 200");
            XCTAssertEqual(httpResponse.URL.absoluteString, url.absoluteString, @"HTTPResponse的 URL 应该与请求的 URL 一致");
        } else {
            XCTFail(@"返回内容不是 NSHTTPURLResponse 类型，直接挂");
        }
        
        // XCTestExpectation 条件已满足，需要通过 fulfill 方法告知异步任务异常已不会发生。
        [expectation fulfill];
    }];
    
    [task resume];
    // 超时后执行
    [self waitForExpectationsWithTimeout:0.5 handler:^(NSError * _Nullable error) {
        [task cancel];
    }];
}

- (void)testButtonBackgroundImageChangesWithNSPredicate {
    XCTAssertNil([self.button backgroundImageForState:UIControlStateNormal], @"self.button 不应该在当前状态有初始背景视图");
    NSPredicate *predicate = [NSPredicate predicateWithBlock:^BOOL(UIButton  * _Nonnull button, NSDictionary<NSString *,id> * _Nullable bindings) {
        return [button backgroundImageForState:UIControlStateNormal] != nil;
    }];
    
    XCTestExpectation * exception = [self expectationForPredicate:predicate evaluatedWithObject:self.button handler:nil];
    [exception setExpectationDescription:@"self.button 没有被设置 backgroundImage"];
    
    // 某些业务逻辑需要对self.button 添加背景视图
    [self.button setBackgroundImage:[UIImage imageNamed:@"button_test_img.png"] forState:UIControlStateNormal];
    
    [self waitForExpectationsWithTimeout:5 handler:nil];
    
}


#pragma mark 性能测试
- (void)testFuncPerformanceExampel
{
    /*
     我们有很多工具库，比如处理图片圆角、条形码生成、图片压缩等，特别是有多方案时，可以衡量不同方案性能
     */
    [self measureBlock:^{
        // Put the code you want to measure the time of here.
        [self _funcPerformanceDemo:10000];
    }];
}

- (void)_funcPerformanceDemo:(NSInteger)maxValue
{
    NSMutableArray * mutArr = [NSMutableArray array];
    for (NSInteger index = 2; index < maxValue; index++)
    {
        BOOL isAdd = YES;
        for (NSInteger tmp = 2; tmp < index; tmp++)
        {
            if (index%tmp == 0 && tmp != index) {
                isAdd = NO;
                break;
            }
        }
        if (isAdd == YES) {
            [mutArr addObject:@(index)];
        }
    }
    NSLog(@"%@",@(mutArr.count));
}

@end
